package com.dixon.worddayday.controller;

import com.dixon.worddayday.bo.CustomerSession;
import com.dixon.worddayday.dto.ProgressResult;
import com.dixon.worddayday.dto.ReviewListResult;
import com.dixon.worddayday.dto.ReviewResult;
import com.dixon.worddayday.model.Customer;
import com.dixon.worddayday.model.Progress;
import com.dixon.worddayday.model.Words;
import com.dixon.worddayday.request.*;
import com.dixon.worddayday.response.BaseRes;
import com.dixon.worddayday.service.ProgressService;
import com.dixon.worddayday.service.WordsService;
import com.dixon.worddayday.util.WordFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class ApiController extends BaseController{
    @Autowired
    private WordsService wordsService;

    @Autowired
    private ProgressService progressService;


    @PostMapping("/api/add")
    public BaseRes add(@RequestBody @Valid AddReq addReq) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        Customer customer = customerSession.getCustomer();
        wordsService.addWord(customer, addReq.getWord(), addReq.getConfirm());

        return BaseRes.success();
    }

    @PostMapping("/api/del")
    public BaseRes del(@RequestBody @Valid DelReq req) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        Customer customer = customerSession.getCustomer();
        progressService.delete(customer, req.getProgressId());

        return BaseRes.success();
    }

    /**
     *
     * @param reviewReq
     * @return
     */
    @GetMapping("/api/review")
    public BaseRes<ReviewListResult> review(@ModelAttribute @Valid BaseReq reviewReq) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        List<Progress> reviewList = wordsService.getReviewWords(customerSession.getCustomer());
        List<Progress> forgotList = wordsService.getForgotWords(customerSession.getCustomer());

        return new BaseRes<>(new ReviewListResult(reviewList, forgotList));
    }

    @PostMapping("/api/review")
    public BaseRes<ReviewResult> review(@RequestBody @Valid ReviewReq reviewReq) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        ReviewResult result = wordsService.review(customerSession.getCustomer(), reviewReq);

        return new BaseRes<>(result);
    }

    @PostMapping("/api/progress")
    public BaseRes<ProgressResult> review(@RequestBody @Valid ProgressReq progressReq) {

        ProgressResult result = new ProgressResult();
        result.setCurrentPage(progressReq.getPage());

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        Page<Progress> myProgress = progressService.getMyProgress(customerSession.getCustomer(), progressReq);
        result.setTotal(myProgress.getTotalElements());
        result.setProgressList(myProgress.getContent());
        result.setHasNext(result.getProgressList().size() == progressReq.getSize());

        result.getProgressList().stream().forEach(e -> {
            WordFormatUtil.formatWordExampleSentence(e.getWords());
        });

        return new BaseRes<>(result);
    }

    @PostMapping("/api/progress/detail")
    public BaseRes<Progress> progressDetail(@RequestBody @Valid ProgressDetailReq progressDetailReq) {

        Progress result = progressService.detail(progressDetailReq.getProgressId());

        return new BaseRes<>(result);
    }

    @GetMapping("/api/randomword/{size}")
    public BaseRes<List<Words>> randomWord(@ModelAttribute @Valid BaseReq req, @PathVariable Integer size) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();
        return new BaseRes<>(progressService.randomWord(customerSession.getCustomer(), size));
    }

    @PostMapping("/api/saveTestResult")
    public BaseRes saveTestResult(@RequestBody @Valid TestResultReq req) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        CustomerSession customerSession = (CustomerSession) auth.getPrincipal();

        wordsService.recordTest(customerSession.getCustomer(), req);

        return BaseRes.success();
    }
}
